﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using entidades;
using System.Data;

namespace Repositorio.SQLRepositorio
{
    public class BlitzSQLRepositorio : IBlitzRepositorio
    {
        public void Create(Blitz entidade) 
        {
            SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=LeiSecaMaps;Integrated Security=SSPI;");

            MyConnection.Open();

            String MyString = @"INSERT INTO Blitz(Endereco, Numero, Cidade, Estado, DataHora, CEP, Geocode)VALUES('" + entidade.Endereco + "','" + entidade.Numero + "','" + entidade.Cidade + "','" + entidade.Estado + "','" + entidade.DataHora + "','" + entidade.CEP + "','" + entidade.Geocode + "'" + ")";
            SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

            MyCmd.ExecuteScalar();
            MyConnection.Close();
        }

        public List<Blitz> ObterBlitzs() 
        {
            List<Blitz> ListaBlitz = new List<Blitz>();
            SqlConnection MySqlConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=LeiSecaMaps;Integrated Security=SSPI;");            

            SqlCommand MySqlComand = MySqlConnection.CreateCommand();
            MySqlComand.CommandText = "select * from Blitz";
            SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();

            MySqlDataAdapter.SelectCommand = MySqlComand;

            DataSet MyDataSet = new DataSet();
            
            MySqlConnection.Open();

            MySqlDataAdapter.Fill(MyDataSet, "LeiSecaMaps");

            foreach (DataRow MyDataRow in MyDataSet.Tables["LeiSecaMaps"].Rows)
            {
                ListaBlitz.Add(new Blitz(Convert.ToInt32(MyDataRow["BlitzId"]), Convert.ToString(MyDataRow["Endereco"]), Convert.ToInt32(MyDataRow["Numero"]), Convert.ToString(MyDataRow["Cidade"]), Convert.ToString(MyDataRow["Estado"]), Convert.ToDateTime(MyDataRow["DataHora"]), Convert.ToString(MyDataRow["CEP"]), Convert.ToString(MyDataRow["Geocode"])));                
            }
            
            return ListaBlitz;
        }
    }
}
